<template>
  <div style="background: #ececec; padding: 30px">
    <!-- 面包屑导航区 -->
    <a-breadcrumb>
      <a-breadcrumb-item>综合管理</a-breadcrumb-item>
      <a-breadcrumb-item>学生管理</a-breadcrumb-item>
    </a-breadcrumb>
    <a-card>
      <!-- 搜索与添加区域 -->
      <a-row :gutter="20">
        <a-col :span="8">
          <el-input
            placeholder="请输入学生姓名"
            v-model="query"
            clearable
            @clear="getStuList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getStuList">
            </el-button>
          </el-input>
        </a-col>
        <a-col :span="4"
          ><el-button type="success" @click="addDialogVisible = true"
            >添加学生
          </el-button></a-col
        >
      </a-row>
      <!-- 用户列表区域 -->
      
      <el-table :data="userList" border stripe>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="电话" prop="telephone"></el-table-column>
        <el-table-column label="地址" prop="location"></el-table-column>
        <el-table-column label="专业" prop="major_id"></el-table-column>
        <el-table-column label="学院" prop="academy_id"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!-- 删除和分配角色按钮 -->
            <el-tooltip
              effect="dark"
              content="修改学生"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope.row.id)"
              >
              </el-button>
            </el-tooltip>

            <el-tooltip
              effect="dark"
              content="删除学生"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSizee"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </a-card>

    <!-- 添加用户的对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="id" prop="id">
          <el-input v-model="addForm.id"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="addForm.sex"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="addForm.telephone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="location">
          <el-input v-model="addForm.location"></el-input>
        </el-form-item>
        <el-form-item label="专业id" prop="major_id">
          <el-input v-model="addForm.major_id"></el-input>
        </el-form-item>
        <el-form-item label="学院id" prop="academy_id">
          <el-input v-model="addForm.academy_id"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStu">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑修改用户的对话框 -->
    <el-dialog
      title="编辑用户"
      :visible.sync="showEditDialogvisible"
      width="30%"
    >
      <!-- 修改用户的对话框 -->
     <el-form ref="editFormRef"  v-model="editForm.name" label-width="70px" :rules="editFormRoles">
  <el-form-item label="姓名" >
    <el-input v-model="editForm.name" disabled></el-input>
  </el-form-item>
  <el-form-item label="性别" >
    <el-input v-model="editForm.sex" disabled></el-input>
  </el-form-item>
  <el-form-item label="电话" prop="telephone">
    <el-input v-model="editForm.telephone"></el-input>
  </el-form-item>
  <el-form-item label="地址" prop="location">
    <el-input v-model="editForm.location"></el-input>
  </el-form-item>
  <el-form-item label="专业id" prop="major_id">
    <el-input v-model="editForm.major_id"></el-input>
  </el-form-item>
  <el-form-item label="学院id" prop="academy_id">
    <el-input v-model="editForm.academy_id"></el-input>
  </el-form-item>
  
     </el-form>
        <el-button @click="showEditDialogvisible = false">取 消</el-button>
        <el-button type="primary" @click="showEditDialogvisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template> 

<script>
export default {
  data() {
    //验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regMobile.test(value)) {
        return cb();
      }

      cb(new Error("请输入合法的手机号"));
    };

    return {
      userList: [],
      total: 0,
      pageNum: 0,
      pageSizee: 0,
      query: "",
      //添加学生的表单数据
      addForm: {
        name: "",
        id: "",
        sex: "",
        telephone: "",
        location: "",
        username: "",
        major_id: 0,
        academy_id: 0,
      },
      //添加表单的验证规则对象
      addFormRules: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
          {
            min: 2,
            max: 5,
            message: "请输入正确的姓名",
            trigger: "blur",
          },
        ],
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        sex: [{ required: true, message: "请输入正确的性别", trigger: "blur" }],
        telephone: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkMobile },
        ],
        location: [{ required: true, message: "请输入住址", trigger: "blur" }],
        username: [
          {
            required: true,
            message: "请输入正确的用户名",
            trigger: "blur",
          },
          {
            min: 5,
            max: 13,
            message: "长度在5-13之间",
            trigger: "blur",
          },
        ],
        major_id: [
          { required: true, message: "请输入正确的专业", trigger: "blur" },
        ],
        academy_id: [
          {
            required: true,
            message: "请输入正确的学院",
            trigger: "blur",
          },
        ],
      },
      //控制添加学生对话框的显示与隐藏
      addDialogVisible: false,
      showEditDialogvisible: false,
      //查询到的用户信息对象
      editForm: {},
      //编辑学生的验证信息
      editFormRoles: {
        telephone: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkMobile },
        ],
        location: [{ required: true, message: "请输入住址", trigger: "blur" }],
        major_id: [
          { required: true, message: "请输入正确的专业id", trigger: "blur" },
        ],
        academy_id: [
          {
            required: true,
            message: "请输入正确的学院id",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getStuList();
  },
  methods: {
    async getStuList() {
      const res = await this.$http.get("/student", {
        params: { name: this.query },
      });
      // console.log(res);
      if (res.status !== 200) {
        return this.$message.error("获取学生列表失败");
      } else {
        // console.log(res.data);
        this.userList = res.data.content;
        this.total = res.data.totalElements;
        this.pageNum = res.data.pageable.pageNumber;
        this.pageSizee = res.data.pageable.pageSize + 1;
      }
    },
    //监听页面容量 pageSize的改变
    handleSizeChange(newSize) {
      console.log(newSize);
    },
    //监听页码值的改变
    handleCurrentChange(newPage) {
      console.log(newPage);
    },
    //监听添加学生对话框的关闭
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //点击按钮  添加学生
    addStu() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起添加学生的网络请求
        const { data: res } = await this.$http.post("student", this.addForm);
        // console.log(res);
        if (res.code < 0) {
          return this.$message.error("添加用户失败");
        } else {
          this.$message.success("添加用户成功");
          //隐藏添加用户的对话框
          this.addDialogVisible = false;
          //添加用户成功  就要刷新列表
          this.getStuList();
        }
      });
    },
    //展示编辑用户的对话框
    async showEditDialog(id) {
      // console.log(id);
      const { data: res } = await this.$http.get("student?id=" + id);
      console.log(res);
      if (res.code < 0 && res.content.length == 0) {
        return this.$message.error("请求失败");
      }
      this.editForm = res.content[0];
      // console.log(res.content[0]);

      this.showEditDialogvisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
</style>